import { Request, Response } from 'express';
import goodsService from '../../services/goods.service'
export class Controller {
    // 获取所有类别接口
    get(_req: Request, res: Response): void {
        goodsService.getAllCategory().then((data)=>{
            res.status(200).json({ msg: 'ok',datas: data })
        }).catch((error)=>{
            res.status(200).json({ msg: 'error', error: error })
        })
    }
    // 新增类别接口
    add(req: Request, res: Response): void {
        // 解析请求参数
        let name = req.body.name
        let description = req.body.description
        let sort = req.body.sort
        goodsService.addCategory({ name, description, sort }).then(()=>{
            res.status(200).json({ msg: 'ok' })
        }).catch((error)=>{
            res.status(200).json({ msg: 'error', error: error })
        })
    }
    // 更类别接口
    update(req: Request, res: Response): void {
        let name = req.body.name
        let id = req.body.id
        let description = req.body.description
        let sort = req.body.sort
        if (sort == undefined) {
            sort = 0
        }
        if (!id) {
            res.status(200).json({ msg:'error', error:'请指定要更新的类别id' })
        }
        goodsService.updateCategory({name, id, description, sort}).then(()=>{
            res.status(200).json({ msg:'ok' })
        })
    }

    // 新增商品
    goodsAdd(req: Request, res: Response): void {
        let name = req.body.name
        let description = req.body.description
        let detail = req.body.detail
        let category_id = req.body.category.id
        let price = req.body.price
        let discounted_price = req.body.discounted_price
        let stock = req.body.stock
        let image = req.body.image
        let status = req.body.status
        let brand = req.body.brand
        goodsService.addGoods({ name, description, detail, category_id, price, discounted_price, stock, image, status, brand }).then(()=>{
            res.status(200).json({ msg: 'ok' })
        }).catch((error)=>{
            res.status(200).json({ msg: 'error', error: error })
        })
    }

    // 更新商品
    goodsUpdate(req: Request, res: Response): void {
        let id = req.body.id
        let name = req.body.name
        let description = req.body.description
        let detail = req.body.detail
        let category_id = req.body.category.id
        let price = req.body.price
        let discounted_price = req.body.discounted_price
        let stock = req.body.stock
        let image = req.body.image
        let status = req.body.status
        let brand = req.body.brand
        goodsService.updateGoods({ id, name, description, detail, category_id, price, discounted_price, stock, image, status, brand }).then(()=>{
            res.status(200).json({ msg: 'ok' })
        }).catch((error)=>{
            res.status(200).json({ msg: 'error', error: error })
        })
    }
    // 删除商品
    goodsDelete(req: Request, res: Response): void {
        let id = req.body.id
        goodsService.deleteGoods(id).then(()=>{
            res.status(200).json({ msg: 'ok' })
        }).catch((error)=>{
            res.status(200).json({ msg: 'error', error: error })
        })
    }

    // 获取商品详情接口
    goodsGet(req: Request, res: Response): void {
        let id = Number(req.query.id as string)
        goodsService.getGoodsDetail(id).then((data)=>{
            res.status(200).json({ msg: 'ok',datas: data })
        }).catch((error)=>{
            res.status(200).json({ msg: 'error', error: error })
        })
    }

    // 查询商品列表
    goodsListGet(req: Request, res: Response): void {
        let cid = undefined
        if (req.query.cid) {
            cid = Number(req.query.cid as string)
        }
        let offset = Number(req.query.offset as string)
        let limit = Number(req.query.limit as string)
        goodsService.getGoodsList(cid, offset, limit).then((data)=>{
            res.status(200).json({ msg: 'ok',datas: data })
        }).catch((error)=>{
            res.status(200).json({ msg: 'error', error: error })
        })
    }

    // 搜索商品
    goodsListSearch(req: Request, res: Response): void {
        // 获取请求中的关键词，分页参数
        let keyword = req.query.keyword as string
        let offset = Number(req.query.offset as string)
        let limit = Number(req.query.limit as string)
        goodsService.searchGoods(keyword, offset, limit).then((data)=>{
            res.status(200).json({ msg: 'ok',datas: data })
        }).catch((error)=>{
            res.status(200).json({ msg: 'error', error: error })
        })
    }

}
export default new Controller();